package com.flyoil.petromp.ui.activity.activity_notice;

import android.content.Intent;
import android.os.Handler;

import com.cnpc.fyutils.ToastUtils;
import com.flyoil.petromp.Constant.ConstantNetworkCode;
import com.flyoil.petromp.R;
import com.flyoil.petromp.adapter.adapter_notice.ItemNoticeAdapter;
import com.flyoil.petromp.apiservice.HttpModel;
import com.flyoil.petromp.apiservice.OnObserverRetrofitResetCallBack;
import com.flyoil.petromp.base.BaseActivity;
import com.flyoil.petromp.base.BaseEntity;
import com.flyoil.petromp.base.BaseRecyAdapter;
import com.flyoil.petromp.entity.entity_notice.NoticeEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 公告列表界面
 */
public class NoticeListActivity extends BaseActivity {
    private ItemNoticeAdapter adapter;

    @Override
    protected int getContentViewResId() {
        return R.layout.activity_notice_list;
    }

    @Override
    protected void initViews() {
        super.initViews();
        setTextTitleName("公告");
        recyclerView = $(R.id.list_notice_list);
        setRecyviewLayoutManager(null);
        adapter = new ItemNoticeAdapter(mContext);
        recyclerView.setAdapter(adapter);
        httpModel = new HttpModel();

    }

    @Override
    protected void initEvents() {
        super.initEvents();
        adapter.setOnItemClickListener(onItemClickListener);
        setBackOnclickListner(mContext);
        setOnRefreshListener(true);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!isFirstLoadck) {
            onRefreshData();
        }
    }

    @Override
    protected void initdatas() {
        super.initdatas();
        Map<String, Object> map = new HashMap<>();
        map.put("pageNumber", pageNumber);
        map.put("pageSize", pageSize);
        httpModel.getNoticeList(map, new OnObserverRetrofitResetCallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity model) {
                super.onSuccess(model);
                NoticeEntity entity = null;
                if (model instanceof NoticeEntity) {
                    entity = (NoticeEntity) model;
                } else {
                    ToastUtils.show_toast(model.getMessage());
                }
                if (entity != null && entity.getData() != null && entity.getData().getList() != null && model.getCode() == ConstantNetworkCode.SuccedCode) {
                    if (isFirstLoadck) {
                        isFirstLoadck = false;
                        adapter.addBottonDatas(entity.getData().getList());
                        recyclerView.notifyDataSetChanged();
                        if (entity.getData().getList().size() > 0) {
                            onDataSucceed();
                            pageNumber++;
                        } else {
                            onDataNull(null);
                        }
                    } else {
                        if (pageNumber == 1) {
                            adapter.setDataList(entity.getData().getList());
                            recyclerView.notifyDataSetChanged();
                            onRefreshDataSucceed();
                        } else {
                            adapter.addBottonDatas(entity.getData().getList());
                            recyclerView.notifyDataSetChanged();
                            if (entity.getData().getList().size() < pageSize) {
                                onLoadDataFinish();
                            } else {
                                onLoadDataSucceed();

                            }
                        }
                        pageNumber++;
                    }

                } else {
                    if (isFirstLoadck) {
                        onDataNull(null);
                    } else {
                        if (pageNumber == 1) {
                            onRefreshDataError();
                        } else {
                            onLoadDataError();
                        }
                    }

                }

            }

            @Override
            public void onFailure(String msg) {
                if (isFirstLoadck) {
                    onDataNull(null);
                } else {
                    if (pageNumber == 1) {
                        onRefreshDataError();
                    } else {
                        onLoadDataError();
                    }
                }
            }
        });
    }

    @Override
    protected void onRefreshData() {
        super.onRefreshData();
        initdatas();
    }

    @Override
    protected void onLoadData() {
        super.onLoadData();
        initdatas();
    }

    private BaseRecyAdapter.OnItemClickListener onItemClickListener = new BaseRecyAdapter.OnItemClickListener() {
        @Override
        public void Onclick(int position, Object data) {
            NoticeEntity.DataBean.ListBean entity = (NoticeEntity.DataBean.ListBean) data;
            Intent intent = new Intent(mContext, NoticeMessageActivity.class);
            intent.putExtra("id", entity.getId());
            startActivity(intent);
        }
    };


}
